<template>
	<view class="content" @click="allClick">
		<view class="bg"></view>
		<view class="box">
			<view class="box_1">
				<uni-icons type="search" size="30"></uni-icons>
				<input type="text" v-model="seachText" placeholder="请输入名称搜索" confirm-type="search" @confirm="changeSeach" />
			</view>
		</view>

		<view class="box1">
			<scroll-view class="scroll-view_H" :scroll-x="true" :scroll-into-view="tabIntoView">
				<view class="box1_1" v-for="(item, index) in tabData" :key="index" :class="{ active: item.id == tabindex }" :id="'tab' + item.id" @click="changeTab(index, item)">
					<text>{{ item.name }}</text>
				</view>
			</scroll-view>
		</view>

		<view class="box2" id="mainbox">
			<view class="box2_l">
				<scroll-view :scroll-y="true" class="scroll_Y_l">
					<view
						class="scroll-view-item"
						v-for="(item, index) in leftData"
						:key="index"
						:class="{ active: item.id == leftindex }"
						@click="changeLeft(index, item)"
						@longpress="changeType(index, item)"
					>
						{{ item.name }}
					</view>
				</scroll-view>
				<view
					class="tooltips"
					v-if="tooltipsShow"
					:style="{
						'--top': tooltipsTop,
						'--left': lleft,
						'--aftertop': tooltipsAfterTop,
						'--afterleft': lafterleft,
						'--lwidth': lwidth,
						'--lheight': lheight,
						'--flexdirection': lflex,
						'--bordercolor': lborder
					}"
				>
					<view class="tooltips_item" @click="handleEdit(1)">
						<image src="../../static/icon/icon9.png" mode="heightFix"></image>
						<text>重命名</text>
					</view>
					<view class="tooltips_item" @click="handleDel(1)">
						<image src="../../static/icon/icon10.png" mode="heightFix"></image>
						<text>删除</text>
					</view>
				</view>
			</view>

			<view class="box2_r">
				<scroll-view :scroll-y="true" class="scroll_Y_r">
					<view class="box4_1" v-for="(item, index) in mainData" :key="index" @click="changeMusicItem(item, index)" @longpress="changeSongs(index, item)">
						<view class="box4_1_l">
							<view>{{ item.name }}</view>
							<view>
								<text>作者：{{ item.author || '--' }}</text>
								<text></text>
							</view>
						</view>
						<uni-icons type="right" size="20" color="#999999"></uni-icons>
					</view>
				</scroll-view>
				<view
					class="tooltips"
					v-if="tooltipsShow1"
					:style="{
						'--top': tooltipsTop1,
						'--left': rleft,
						'--aftertop': rftertop,
						'--afterleft': rafterleft,
						'--lwidth': rwidth,
						'--lheight': rheight,
						'--flexdirection': rflex,
						'--bordercolor': rborder
					}"
				>
					<view class="tooltips_item" @click="handleEdit(2)">
						<image src="../../static/icon/icon9.png" mode="heightFix"></image>
						<text>重命名</text>
					</view>
					<view class="tooltips_item" @click="handleDel(2)">
						<image src="../../static/icon/icon10.png" mode="heightFix"></image>
						<text>删除</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import API from '@/utils/api.js';
import { handleTree } from '@/utils/index.js';
export default {
	data() {
		return {
			seachText: '',
			tabindex: 0,
			tabIntoView: '',
			tabData: [],
			leftindex: 0,
			leftData: [],
			mainData: [],

			manboxHeight: 0,
			longpressData: null,
			longpressIndex: null,

			tooltipsShow: false,
			tooltipsTop: '0px',
			tooltipsAfterTop: '20px',

			tooltipsShow1: false,
			tooltipsTop1: '90px'
		};
	},
	computed: {
		lleft() {
			return uni.upx2px(210) + 'px';
		},
		lwidth() {
			return uni.upx2px(100) + 'px';
		},
		lheight() {
			return uni.upx2px(300) + 'px';
		},
		lflex() {
			return 'column';
		},
		lborder() {
			return 'transparent rgba(0, 0, 0, 0.5) transparent transparent';
		},
		lafterleft() {
			return '0px';
		},
		rleft() {
			return '20px';
		},
		rwidth() {
			return uni.upx2px(300) + 'px';
		},
		rheight() {
			return uni.upx2px(100) + 'px';
		},
		rflex() {
			return 'row';
		},
		rborder() {
			return 'transparent transparent rgba(0, 0, 0, 0.5) transparent';
		},
		rftertop() {
			return '0px';
		},
		rafterleft() {
			return uni.upx2px(300 - 20) / 2 + 'px';
		}
	},
	methods: {
		allClick() {
			this.tooltipsShow = false;
			this.tooltipsShow1 = false;
		},

		// 搜索
		changeSeach() {
			if (!this.seachText) return;
			uni.navigateTo({
				url: '/pages/index/seachList?title=' + this.seachText,
				complete: () => {
					this.seachText = '';
				}
			});
		},

		// 切换一级菜单
		changeTab(index, item) {
			this.tabindex = item.id;
			console.log('左侧的数据', this.tabData[index]);
			this.leftindex = this.tabData[index].children ? this.tabData[index].children[0].id : 0;
			this.leftData = item.children;
			this.getSongs();
		},

		// 切换侧边菜单
		changeLeft(index, item) {
			this.leftindex = item.id;
			this.getSongs();
		},

		// 查看乐谱详情
		changeMusicItem(item, index) {
			console.log(item, index, '查看歌曲');
			uni.setStorageSync('id', this.tabindex);
			uni.setStorageSync('lid', this.leftindex);
			uni.navigateTo({
				url: `/pages/ownmusic/detail?id=${item.id}`
			});
		},

		// 二级分类长按
		changeType(index, item) {
			if (item.user == 0) return;
			this.tooltipsShow = true;
			this.tooltipsShow1 = false;
			this.longpressData = item;
			this.longpressIndex = index;
			let topnum = index * uni.upx2px(105);
			if (topnum > this.manboxHeight) {
				this.tooltipsTop = this.manboxHeight - uni.upx2px(300) + 'px';
				this.tooltipsAfterTop = uni.upx2px(300) - (uni.upx2px(300) - 20) + 'px';
			} else {
				this.tooltipsTop = topnum + 'px';
				this.tooltipsAfterTop = '20px';
			}
			console.log('长按的参数', index, this.tooltipsTop);
		},

		// 乐谱长按
		changeSongs(index, item) {
			if (item.user == 0) return;
			this.tooltipsShow1 = true;
			this.tooltipsShow = false;
			this.longpressData = item;
			this.longpressIndex = index;
			let topnum = (index + 1) * uni.upx2px(170);
			if (topnum > this.manboxHeight) {
				this.tooltipsTop1 = this.manboxHeight - uni.upx2px(100) + 'px';
			} else {
				this.tooltipsTop1 = topnum + 'px';
			}
			console.log('长按的参数', index, this.tooltipsTop);
		},

		// 删除侧栏、乐谱
		handleDel(val) {
			console.log('删除的类型', val);
			uni.showModal({
				title: '提示',
				content: '确定删除吗？',
				success: (res) => {
					if (res.confirm) {
						if (val == 1) {
							this.delTyps(this.longpressData, this.longpressIndex);
						} else {
							this.delSongs(this.longpressData, this.longpressIndex);
						}
					} else if (res.cancel) {
					}
				}
			});
		},

		// 删除二级分类
		delTyps(item, index) {
			console.log('删除的二级分类数据', item);
			let promeis = [];
			promeis.push(API.deleteRecord(item.id, 0));

			Promise.all(promeis).then(
				(res) => {
					console.log('删除二级分类', res);
					uni.showToast({
						icon: 'success',
						title: '删除成功'
					});
					uni.setStorageSync('id', this.tabindex);
					uni.setStorageSync('lid', this.leftindex);
					this.getList();
				},
				(err) => {
					console.log('删除失败', err);
				}
			);
		},

		// 删除乐谱
		delSongs(item, index) {
			console.log('删除的乐谱数据', item);
			let arr = item.urls.split(',');
			let promeis = [];
			for (let i = 0; i < arr.lengtn; i++) {
				promeis.push(API.deleteRecord(arr[i], 2));
			}
			promeis.push(API.deleteRecord(item.id, 1));
			// API.getFileList(item.id);
			Promise.all(promeis).then(
				(res) => {
					console.log('删除乐谱', res);
					uni.showToast({
						icon: 'success',
						title: '删除成功'
					});
					uni.setStorageSync('id', this.tabindex);
					uni.setStorageSync('lid', this.leftindex);
					this.getList();
				},
				(err) => {
					console.log('删除失败', err);
				}
			);
		},

		// 	重命名
		handleEdit(val) {
			console.log('重命名的类型', val);
			if (val == 1) {
				this.editTyps(this.longpressData, this.longpressIndex);
			} else {
				this.editSongs(this.longpressData, this.longpressIndex);
			}
		},

		// 重命名二级分类
		editTyps(item, index) {
			console.log('重命名的二级分类数据', item);
			uni.showModal({
				title: '提示',
				placeholderText: '请输入新的名称',
				editable: true,
				success: (res) => {
					if (res.confirm) {
						console.log('输入的新名称', res);
						let name = res.content;
						if (name) {
							console.log('新数据', name);
							API.recordSalary({
								id: item.id,
								name: name,
								father_id: item.father_id,
								user: item.user
							}).then((res) => {
								console.log('修改二级分类成功', res);
								uni.showToast({
									icon: 'success',
									title: '修改成功'
								});
								uni.setStorageSync('id', this.tabindex);
								uni.setStorageSync('lid', this.leftindex);
								this.getList();
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '请输入新的名称'
							});
						}
					} else if (res.cancel) {
					}
				}
			});
		},

		// 重命名乐谱
		editSongs(item, index) {
			console.log('重命名的乐谱数据', item);
			uni.showModal({
				title: '提示',
				placeholderText: '请输入新的名称',
				editable: true,
				success: (res) => {
					if (res.confirm) {
						console.log('输入的新名称', res);
						let name = res.content;
						if (name) {
							console.log('新数据', name);
							API.recordSong({
								id: item.id,
								name: name,
								usls: item.urls,
								type: item.type,
								author: item.author,
								album: item.album,
								father_id: item.father_id,
								user: item.user
							}).then((res) => {
								console.log('修改二级分类成功', res);
								uni.showToast({
									icon: 'success',
									title: '修改成功'
								});
								uni.setStorageSync('id', this.tabindex);
								uni.setStorageSync('lid', this.leftindex);
								this.getList();
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '请输入新的名称'
							});
						}
					} else if (res.cancel) {
					}
				}
			});
		},

		// 查询分类数据
		getList() {
			API.getAllCategories().then((res) => {
				console.log('查询的分类数据', res);
				this.tabData = handleTree(res);

				if (uni.getStorageSync('id')) {
					this.tabindex = uni.getStorageSync('id');
					this.tabIntoView = 'tab' + this.tabindex;
					uni.removeStorageSync('id');
					let obj = this.tabData.find((e) => e.id == this.tabindex);
					if (uni.getStorageSync('lid')) {
						this.leftindex = uni.getStorageSync('lid');
						uni.removeStorageSync('lid');
					} else {
						this.leftindex = obj.children ? obj.children[0].id : 0;
					}
					this.leftData = obj.children;
				} else {
					this.tabindex = this.tabData[0].id;
					this.tabIntoView = 'tab' + this.tabindex;
					if (uni.getStorageSync('lid')) {
						this.leftindex = uni.getStorageSync('lid');
						uni.removeStorageSync('lid');
					} else {
						this.leftindex = this.tabData[0].children ? this.tabData[0].children[0].id : 0;
					}
					this.tabIntoView = 'tab' + this.tabindex;
					this.leftData = this.tabData[0].children;
				}
				console.log(res, this.tabData, '获取一级分类');
				this.getSongs();
			});
		},

		// 查询对应的乐谱
		getSongs() {
			console.log('左侧的ID', this.leftindex);
			if (this.leftindex == 0) {
				this.mainData = [];
				return;
			}
			API.getSongsByCategory(this.leftindex).then((res) => {
				console.log(res, '查询的乐谱数据');
				this.mainData = res;
			});
		},

		getMainBoxHeight() {
			let query = uni.createSelectorQuery().in(this);
			query.select('#mainbox').boundingClientRect();
			query.exec((res) => {
				console.log('获取节点的信息', res);
				this.manboxHeight = res[0].height;
			});
		}
	},
	onShow() {
		plus.screen.lockOrientation('portrait-primary');
		this.getList();
		this.$nextTick(() => {
			this.getMainBoxHeight();
		});
	}
};
</script>

<style lang="scss" scoped>
/* 针对手机设备的样式 */
@media only screen and (max-width: 600px) {
	.content {
		width: 100vw;
		padding-top: var(--status-bar-height);
		position: relative;
		overflow: hidden;

		.bg {
			width: calc(100vw + 270rpx);
			height: 282rpx;
			background-color: rgba(23, 183, 109, 0.61);
			filter: blur(180rpx);
			position: absolute;
			top: 0rpx;
			left: -210rpx;
			z-index: 1;
		}

		.box {
			width: 100%;
			margin-bottom: 14rpx;
			padding: 0 22rpx;
			margin-top: 30rpx;
			position: relative;
			z-index: 2;
			margin-bottom: 20rpx;

			.box_1 {
				width: 100%;
				height: 84rpx;
				background-color: #fff;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				margin-right: 18rpx;
				display: flex;
				align-items: center;
				padding: 0 28rpx;

				input {
					flex: 1 0;
					padding: 0 20rpx;
				}
			}
		}

		.box1 {
			width: 100%;
			height: 65rpx;
			padding: 0 22rpx;
			margin-bottom: 25rpx;
			position: relative;
			z-index: 2;

			.scroll-view_H {
				width: 100%;
				white-space: nowrap;
				overflow-y: auto;

				.box1_1 {
					display: inline-block;
					min-width: 128rpx;
					height: 65rpx;
					padding: 0 10rpx;
					color: rgb(23, 173, 110);
					background-color: rgb(235, 255, 246);
					margin: 0 12rpx;
					border-radius: 12rpx 12rpx 12rpx 12rpx;
					font-family: Source Han Sans, Source Han Sans;
					font-weight: 500;
					font-size: 28rpx;
					color: #17b76d;
					line-height: 65rpx;
					text-align: center;
				}

				.box1_1:nth-child(1) {
					margin-left: 0;
				}

				.box1_1:nth-last-child(1) {
					margin-right: 0;
				}

				.active {
					color: #fff;
					background-color: rgb(23, 173, 110);
				}
			}
		}

		.box2 {
			width: 100%;
			height: calc(100vh - 225rpx - var(--status-bar-height));
			display: flex;
			justify-content: space-between;
			overflow: hidden;
			background-color: #fff;
			position: relative;
			z-index: 2;

			.box2_l,
			.box2_r {
				position: relative;
			}

			.scroll_Y_l,
			.scroll_Y_r {
				height: calc(100vh - 225rpx - var(--status-bar-height));
			}

			.scroll_Y_l {
				width: 190rpx;
				border-right: 1rpx solid #dbdbdb;
				border-top: 1rpx solid #dbdbdb;

				.scroll-view-item {
					width: 190rpx;
					height: 105rpx;
					font-family: Source Han Sans, Source Han Sans;
					font-weight: 500;
					font-size: 32rpx;
					color: #333333;
					line-height: 105rpx;
					text-align: center;
					border-bottom: 1rpx solid #dbdbdb;
					// 超出部分隐藏
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.active {
					position: relative;

					&::before {
						content: '';
						position: absolute;
						top: 0;
						left: 0;
						width: 8rpx;
						height: 105rpx;
						background-color: #17b76d;
						box-shadow: 15rpx 0rpx 50rpx #17b76d;
					}
				}
			}

			.scroll_Y_r {
				width: calc(100vw - 205rpx);
				padding: 20rpx 0;

				.box4_1 {
					width: calc(100% - 20rpx);
					height: 144rpx;
					box-shadow: 0rpx 6rpx 16rpx 0rpx rgba(0, 0, 0, 0.08);
					display: flex;
					align-items: center;
					justify-content: space-between;
					background-color: #ffffff;
					padding: 0 18rpx 0 26rpx;
					margin-bottom: 8rpx;

					.box4_1_l {
						flex: 1 0;
						width: 0;

						view:nth-child(1) {
							font-weight: 500;
							font-size: 36rpx;
							color: #333333;
							line-height: 52rpx;
							text-align: left;
							margin-bottom: 16rpx;
						}

						view:nth-child(2) {
							font-weight: 400;
							font-size: 28rpx;
							color: #666666;
							line-height: 40rpx;
							text-align: left;
							display: flex;
							align-items: center;

							text {
								flex: 1;
							}
						}
					}
				}
			}

			.tooltips {
				position: absolute;
				top: var(--top);
				left: var(--left);
				z-index: 9;
				width: var(--lwidth);
				height: var(--lheight);
				padding: 20rpx 0;
				background-color: rgba(0, 0, 0, 0.5);
				border-radius: 10rpx;
				display: flex;
				flex-direction: var(--flexdirection);
				align-items: center;
				justify-content: space-around;

				.tooltips_item {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin: 10rpx 0;

					image {
						height: 50rpx;
					}

					text {
						font-size: 28rpx;
						color: #ffffff;
					}
				}
			}

			.tooltips::after {
				content: '';
				position: absolute;
				top: var(--aftertop);
				left: var(--afterleft);
				transform: translate(-100%, -100%);
				width: 0;
				height: 0;
				border-width: 10rpx;
				border-style: solid;
				border-color: var(--bordercolor);
			}
		}
	}
}

/* 针对平板设备的样式 */
@media only screen and (min-width: 601px) and (max-width: 1024px) {
	.content {
		width: 100vw;
		padding-top: var(--status-bar-height);
		position: relative;
		overflow: hidden;

		.bg {
			width: calc(100vw + 270px);
			height: 282px;
			background-color: rgba(23, 183, 109, 0.61);
			filter: blur(180px);
			position: absolute;
			top: 0px;
			left: -210px;
			z-index: 1;
		}

		.box {
			width: 100%;
			margin-bottom: 14px;
			padding: 0 22px;
			margin-top: 30px;
			position: relative;
			z-index: 2;
			margin-bottom: 20px;

			.box_1 {
				width: 100%;
				height: 84px;
				background-color: #fff;
				border-radius: 12px 12px 12px 12px;
				margin-right: 18px;
				display: flex;
				align-items: center;
				padding: 0 28px;

				input {
					flex: 1 0;
					padding: 0 20px;
				}
			}
		}

		.box1 {
			width: 100%;
			height: 65px;
			padding: 0 22px;
			margin-bottom: 25px;
			position: relative;
			z-index: 2;

			.scroll-view_H {
				width: 100%;
				white-space: nowrap;
				overflow-y: auto;

				.box1_1 {
					display: inline-block;
					min-width: 128px;
					height: 65px;
					padding: 0 10px;
					color: rgb(23, 173, 110);
					background-color: rgb(235, 255, 246);
					margin: 0 12px;
					border-radius: 12px 12px 12px 12px;
					font-family: Source Han Sans, Source Han Sans;
					font-weight: 500;
					font-size: 28px;
					color: #17b76d;
					line-height: 65px;
					text-align: center;
				}

				.box1_1:nth-child(1) {
					margin-left: 0;
				}

				.box1_1:nth-last-child(1) {
					margin-right: 0;
				}

				.active {
					color: #fff;
					background-color: rgb(23, 173, 110);
				}
			}
		}

		.box2 {
			width: 100%;
			height: calc(100vh - 225px - var(--status-bar-height));
			display: flex;
			justify-content: space-between;
			overflow: hidden;
			background-color: #fff;
			position: relative;
			z-index: 2;

			.box2_l,
			.box2_r {
				position: relative;
			}

			.scroll_Y_l,
			.scroll_Y_r {
				height: calc(100vh - 225px - var(--status-bar-height));
			}

			.scroll_Y_l {
				width: 190px;
				border-right: 1px solid #dbdbdb;
				border-top: 1px solid #dbdbdb;

				.scroll-view-item {
					width: 190px;
					height: 105px;
					font-family: Source Han Sans, Source Han Sans;
					font-weight: 500;
					font-size: 32px;
					color: #333333;
					line-height: 105px;
					text-align: center;
					border-bottom: 1px solid #dbdbdb;
					// 超出部分隐藏
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.active {
					position: relative;

					&::before {
						content: '';
						position: absolute;
						top: 0;
						left: 0;
						width: 8px;
						height: 105px;
						background-color: #17b76d;
						box-shadow: 15px 0px 50px #17b76d;
					}
				}
			}

			.scroll_Y_r {
				width: calc(100vw - 205px);
				padding: 20px 0;

				.box4_1 {
					width: calc(100% - 20px);
					height: 144px;
					box-shadow: 0px 6px 16px 0px rgba(0, 0, 0, 0.08);
					display: flex;
					align-items: center;
					justify-content: space-between;
					background-color: #ffffff;
					padding: 0 18px 0 26px;
					margin-bottom: 8px;

					.box4_1_l {
						flex: 1 0;
						width: 0;

						view:nth-child(1) {
							font-weight: 500;
							font-size: 36px;
							color: #333333;
							line-height: 52px;
							text-align: left;
							margin-bottom: 16px;
						}

						view:nth-child(2) {
							font-weight: 400;
							font-size: 28px;
							color: #666666;
							line-height: 40px;
							text-align: left;
							display: flex;
							align-items: center;

							text {
								flex: 1;
							}
						}
					}
				}
			}

			.tooltips {
				position: absolute;
				top: var(--top);
				left: var(--left);
				z-index: 9;
				width: var(--lwidth);
				height: var(--lheight);
				padding: 20px 0;
				background-color: rgba(0, 0, 0, 0.5);
				border-radius: 10px;
				display: flex;
				flex-direction: var(--flexdirection);
				align-items: center;
				justify-content: space-around;

				.tooltips_item {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin: 10px 0;

					image {
						height: 50px;
					}

					text {
						font-size: 28px;
						color: #ffffff;
					}
				}
			}

			.tooltips::after {
				content: '';
				position: absolute;
				top: var(--aftertop);
				left: var(--afterleft);
				transform: translate(-100%, -100%);
				width: 0;
				height: 0;
				border-width: 10px;
				border-style: solid;
				border-color: var(--bordercolor);
			}
		}
	}
}
</style>
